package com.ms.service.module.commonsettings;

import com.google.gson.Gson;
import com.xpec.c4.game.utility.GameUtility;

/**
 * 共用設定類型<br>
 * <br>
 * 新增Type時請檢查以下需要新增的部分:
 * 1. 新增設定本身的class(用來轉json與讀取設定使用)
 * 2. 將新增的class加入CommonSettings (com.xpec.c4.service.commonsettings.CommonSettings)<br>
 * 3. 初始值設定 (com.xpec.c4.service.commonsettings.CommonSettingType.getDefaultSetting())<br>
 * 4. 要照field順序新增更新給client的updateID (com.xpec.c4.service.commonsettings.SUpdateCommonSettingsMessage)<br>
 * 5. 在GMTOOL要提供新增與修改設定的方法(case:61)
 *
 */
public enum CommonSettingType {

	/**虛寶交易*/
	SEND_GIFT(
				1,
				CommonSendGiftSetting.class,
				true,
				SUpdateCommonSettingsMessage.SEND_GIFT_FEE,
				SUpdateCommonSettingsMessage.SEND_GIFT_MONEY,
				SUpdateCommonSettingsMessage.SEND_GIFT_LEVEL
			 ),
	
	/**重複名稱檢查*/
	DOUBLE_NAME_CHECK(
				2,
				CommonDoubleNameSetting.class,
				true,
				SUpdateCommonSettingsMessage.DOUBLE_NAME_CHECK
			 ),
	
	/**
	 * 帳號登入擋ip功能
	 */
	ACCOUNT_LOGIN_BLOCK_IP(
				3, 
				CommonAccountLoginBlockIPSetting.class,
				false
			),
			
	/**
	 * 儲值限制金額
	 */
	PURCHASE_LIMIT(
				4,
				CommonPurchaseLimitSetting.class,
				false
			),
			
	/**
	 * 儲值限制金額
	 */
	SEND_ITEM(
				6,
				CommonSendItemSetting.class,
				false
			),
			;
	
	/**
	 * 預設值設定<br>
	 * (在server啟動時發現DB沒設定時自動添加的值)
	 */
	public Object getDefaultSetting(){
		switch (this) {
		case SEND_GIFT:{
			CommonSendGiftSetting setting = new CommonSendGiftSetting();
			setting.setFee(2.0f);
			setting.setLimitMinLevel(50);
			setting.setLimitMinMoney(30000);
			return setting;
		}
		
		case DOUBLE_NAME_CHECK:{
			CommonDoubleNameSetting setting = new CommonDoubleNameSetting();
			setting.setCheckOpen(true);
			return setting;
		}
		
		case ACCOUNT_LOGIN_BLOCK_IP: {
			CommonAccountLoginBlockIPSetting setting = new CommonAccountLoginBlockIPSetting();
			setting.setOpen(false);
			return setting;
		}			
		
		case PURCHASE_LIMIT: {
			CommonPurchaseLimitSetting setting = new CommonPurchaseLimitSetting();
			setting.setGooglePlay(-1);
			setting.setAppStore(-1);
			return setting;
		}			
		
		case SEND_ITEM: {
			CommonSendItemSetting setting = new CommonSendItemSetting();
			setting.setDailySendSettings(new DailyLoginSendSettings());
			return setting;
		}	

		default:
			return null;
		}
	}

	/**
	 * @param id (server自己用來辨識的type ID)
	 * @param convertClass (轉成設定的class)
	 * @param sendToAllClient (是否要送給client更新)
	 * @param updateID (有更新異動的ID，傳給client更新用，不可重複)
	 */
	private CommonSettingType(int id, Class<?> convertClass, boolean isSendToAllClient, int... updateID) {
		this.id = id;
		this.convertClass = convertClass;
		
		this.updateID = updateID;
	}
	
	/**
	 * type id
	 */
	private int id;
	
	/**
	 * 更新用的ID
	 */
	private int[] updateID;	
	/**
	 * 用來轉型的class
	 */
	private Class<?> convertClass;
	
	/**
	 * 是否要送給client更新
	 */
	private boolean sendToAllClient;
	
	public int getId(){
		return this.id;
	}
	
	public int[] getUpdateID(){
		return this.updateID;
	}
	
	public Class<?> getConvertClass(){
		return this.convertClass;
	}
	
	public boolean isSendToAllClient() {
		return sendToAllClient;
	}
	
	/**
	 * 從json取得轉換的class
	 */
	public Object getClassFromJson(String jsonStr){
		if(!GameUtility.isHaveString(jsonStr))
			return null;
		
		try {
			return new Gson().fromJson(jsonStr, this.getConvertClass());
		} catch (Exception e) {
			return null;
		}
	}

}
